<?php
declare(strict_types=1);
 
namespace App\JsonRpc\ServiceProvider;

use App\Constants\ErrorCode;
use App\Utils\ApiUtils;
use App\Validate\LotteryRpcValidation;
use Hyperf\RpcServer\Annotation\RpcService;
use App\Utils\ServerUtil;
use Hyperf\Utils\ApplicationContext;

/**
 * 注意，如希望通过服务中心来管理服务，需在注解内增加 publishTo 属性
 * @RpcService(name="LotteryService", protocol="jsonrpc", server="jsonrpc")
 */
class LotteryService implements LotteryServiceInterface
{
    public function consume($data): string{
        $data = json_decode($data,true);
        ServerUtil::info('rpc-LotteryService-进入', ['path'=>'LotteryService', 'parameter'=>$data]);
        ApplicationContext::getContainer()->get(LotteryRpcValidation::class)->check($data, 'lotteryService')->response();

        $obj = ApplicationContext::getContainer()->get(\App\Service\LotteryService::class);

        if($obj->addTierLottery($data['openid'], $data['order_no'])){
            $responseData = json_encode(ApiUtils::send(ErrorCode::SUCCESS,'操作成功'));
        }else{
            $responseData = json_encode(ApiUtils::send(ErrorCode::ERR_BUESSUS,'用户不存在'));
        }

        ServerUtil::info('rpc-LotteryService-退出', ['path'=>'LotteryService','param'=>$data,'res'=>$responseData]);
        return $responseData;
    }
}
